<?php

require_once("dao/forecast.dao.php");
require_once("dao/placeFromSql.dao.php");
require_once("storage/predefRoutes.php");
require_once("model/routeCollection.class.php");
require_once("model/route.class.php");
require_once("model/place.class.php");

class route extends abstract_controller {

	function prepare_model($request_params, Smarty $smarty_obj) {
		/**
		 * Steps:
		 * 1: velg fra-til
		 * 2: velg tidspunkt, kjør javascript som henter byer
		 * 3: vis vær
		 */
		
		$date = isset($_POST['date']) ? htmlentities($_POST['date']) : null;
		$hour = isset($_POST['time']) ? htmlentities($_POST['time']) : "";
	
		$from = isset($_POST['from']) ? ($_POST['from']) : "";
		$to   = isset($_POST['to'])   ? ($_POST['to'])   : ""; 
		$start= isset($_POST['start'])? ($_POST['start']): "";
		$end  = isset($_POST['end'])  ? ($_POST['end'])  : "";

		if((empty($from) || empty($to)) && empty($start) && empty($end)){
			$smarty_obj->assign('step1',true);
			
		} else if (empty($start) && empty($end)){
			$smarty_obj->assign('step2',true);
			
			$by1 = placeFromSql::findPlaceName($from);
			$by2 = placeFromSql::findPlaceName($to);
			$n1 = $by1->getName();
			$n2 = $by2->getName();
			if (empty($n1) || empty($n2)){
				$smarty_obj->assign('error','Ugyldig input, gå tilbake og prøv igjen');
			}
			$year  = date('Y');
			$month = date('m');
			$today = date('d');
			if (is_null($date)){
				$hour  = date('H');
				$date  = $year.'-'.$month.'-'.$today;
			}
			if ($hour<10) $hour = '0'.$hour;
			$xmlDate = $year.'-'.$month.'-'.$today.'T'.$hour.':00:00Z';
		
			$dateObj = functions::genereateDateObj($xmlDate);
			$smarty_obj->assign('date',$dateObj);			
			$smarty_obj->assign('ch_day', $date);
			$smarty_obj->assign('ch_time',$hour);	
			$smarty_obj->assign('from',$from);
			$smarty_obj->assign('to',$to);
			$smarty_obj->assign('map24', true); // add js to html	
			
		} else if (!( empty($start) || empty($end))){
			$smarty_obj->assign('step3',true);
			$smarty_obj->assign('day',$date);
			$year  = date('Y');
			$month = date('m');
			$today = date('d');
			$tmp_hour = $hour<10 ? '0'.$hour : $hour;
			$xmlDate = $year.'-'.$month.'-'.$today.'T'.$tmp_hour.':00:00Z';
			
//			echo "<pre>";
//			print_r($_POST);
//			echo "</pre>";

			$startObj = placeFromSql::findPlaceName($start);
			$endObj   = placeFromSql::findPlaceName($end);
			$string1  = $startObj->getName();
			$string2  = $endObj->getName();
			if (!(empty($string1) || empty($string2))){
				$route = new RouteModel($string1, $string2, $xmlDate);
			} else {
				echo "ERROR<br/><pre>";
				print_r($startObj);
				print_r($endObj);
				exit();
			}
			
			$lastCity = $string1;
			
			$route->addPlace($startObj, 0, $xmlDate);
			$i = 0;
			while(isset($_POST["by$i"])){
				$city = substr(($_POST["by$i"]),1);
				$dist = htmlentities($_POST["km$i"]); 
				$place = placeFromSql::findPlaceName($city);
				$pname = $place->getName();
				if (!empty($pname)){
					if ($pname != $lastCity){
						$route->addPlace($place, $dist, $xmlDate);
					}
				}
				$lastCity = $pname;
				$i++;
			}
			//echo $xmlDate;
			$endPlace = placeFromSql::findPlaceName($end);
			$endPlaceName = $endPlace->getName();
			if ($endPlaceName != $lastCity){
				$totalDist = htmlentities($_POST['total']);
				$route->addPlace($endPlace, $totalDist, $xmlDate);
			}
			$route->generateForecast();
			
//			echo "<pre>";
//			print_r($route);
//			exit();

			$datee = explode('T',$xmlDate);
			$smarty_obj->assign('xmlDate',$datee[0]);
			$smarty_obj->assign('route',$route);
			$smarty_obj->assign('days_ahead',$date);
			$smarty_obj->assign('hour_ahead',$hour);

		} else { 
			echo "<pre>";
			print_r($_POST);
			echo "</pre>";
			die("noe gikk veldig galt");	
		}
		$smarty_obj->assign('page_title', 'TurYr: Velg rute');
	}

}

?>
